//
//  HNHomeCouseListCell.m
//  优学荟管理系统
//
//  Created by edz on 2018/1/12.
//  Copyright © 2018年 星道三好. All rights reserved.
//

#import "HNHomeCouseListCell.h"
#import "CircleProgressView.h"
#import <UIButton+WebCache.h>
#import "ImgZoom.h"
#import "EMCDDeviceManager.h"

@implementation HNHomeCouseListCell {
    
    __weak IBOutlet NSLayoutConstraint *lineHeigh;
    __weak IBOutlet NSLayoutConstraint *titHeight;
    __weak IBOutlet UILabel *_title;
    __weak IBOutlet CircleProgressView *_circle;
    __weak IBOutlet UILabel *_type;
    __weak IBOutlet UILabel *_des;
    __weak IBOutlet UILabel *_time;
    __weak IBOutlet NSLayoutConstraint *_btnImgHeight;
    IBOutletCollection(UIImageView) NSArray *_btnImgs;
    
    __weak IBOutlet UIButton *modifyingBtn;
    __weak IBOutlet NSLayoutConstraint *modifyHeight;
    __weak IBOutlet NSLayoutConstraint *lineViewHeight;
}

- (void)awakeFromNib {
    
    [super awakeFromNib];
    
    [_btnImgs enumerateObjectsUsingBlock:^(UIImageView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj addGestureRecognizer:[self tap]];
        [obj addGestureRecognizer:[self longPress]];
    }];
}

- (void)setModel:(ZYHomeCouseListsssModel *)model {
    
    _model = model;
    
    modifyingBtn.hidden = [self.model.flag intValue] == 1&&[[NSString stringWithFormat:@"%@",self.model.count]isEqualToString:@"0"]  ? YES : NO;
    
    if ([self.model.flag intValue] == 1 && [[NSString stringWithFormat:@"%@",self.model.count]isEqualToString:@"0"]) {
       
        modifyHeight.constant = 0;
        lineViewHeight.constant = 0;
    } else {
        modifyHeight.constant = 17;
        lineViewHeight.constant = 1;
    }

    _title.text = model.title;
    _time.text = model.createTime;
    _type.text = model.lessonName;
    _des.text = [NSString stringWithFormat:@"%@", model.content];
    // 圆
    _circle.backgroundColor = [UIColor clearColor];
    if (0 == [model.count integerValue]) {
        
        _circle.percent = 0.0;
        _circle.centerLabel.text = [NSString stringWithFormat:@"%@/%@",model.count,model.totalCount];
        
    }else{
        
        float mm = [model.count floatValue];
        float ff = [model.totalCount floatValue];
        float hh = mm/ff;
        _circle.percent = hh;
        _circle.centerLabel.text = [NSString stringWithFormat:@"%@/%@",model.count,model.totalCount];
        
        if(model.count==model.totalCount)
        {
            modifyingBtn.hidden = YES;
            modifyHeight.constant = 0;
            lineViewHeight.constant = 0;
        }
        
    }
    
    if(model.content.length==0)
    {
      titHeight.constant = 0;
    }
    else
    {
      titHeight.constant = 35;
    }
    // 高度
    if ((model.video != nil && ![model.video isKindOfClass:[NSNull class]] && model.video.length > 0) || model.paths.count > 0) {
         lineHeigh.constant = 1;
        
        _btnImgHeight.constant = 88;
    } else {
        lineHeigh.constant = 0;
        
        _btnImgHeight.constant = 0;
    }
    
    // 图片音频视频
    NSMutableArray *arr = [model.paths mutableCopy];
    if (model.video != nil && ![model.video isKindOfClass:[NSNull class]] && model.video.length > 0) {
        
        [arr addObject:model.video];
    }
    
    for (int i=0; i<_btnImgs.count; i++) {
        
        UIButton *btn = _btnImgs[i];
        btn.hidden = YES;
    }
    

    
    [arr enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
       
        
            UIImageView *btn = _btnImgs[idx];
            btn.hidden = NO;
            NSString *Str = [obj pathExtension];
            if ([Str isEqualToString:@"png"] || [Str isEqualToString:@"jpg"]) {
                
                [btn sd_setImageWithURL:[NSURL URLWithString:obj] placeholderImage:[UIImage imageNamed:@"bg_img_homework_picture_default"]];
                
            } else if ([Str isEqualToString:@"amr"] || [Str isEqualToString:@"mp3"]) {
                
                [btn setImage:[UIImage imageNamed:@"btn_mp3"]];
                
            } else {
                
    
                [btn sd_setImageWithURL:[NSURL URLWithString:model.videoImg] placeholderImage:[UIImage imageNamed:@"bg_img_homework_picture_default"]];
            }
        
    }];
}



- (void)ClickAction:(UITapGestureRecognizer *)sender {
    
    
    // 图片音频视频
    NSMutableArray *arr = [self.model.paths mutableCopy];
    if (self.model.video != nil && ![self.model.video isKindOfClass:[NSNull class]] && self.model.video.length > 0) {
        
        [arr addObject:self.model.video];
    }
    
    [_btnImgs enumerateObjectsUsingBlock:^(UIImageView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if ((UIImageView *)sender.view == obj) {
            
            
            self.ClickImgAction ? self.ClickImgAction(arr[idx], (UIImageView *)sender.view) : nil;
            *stop = YES;
        }
    }];
}

- (void)ClickSaveAction:(UILongPressGestureRecognizer *)sender {

    if (sender.state == UIGestureRecognizerStateBegan) {
    // 图片音频视频
    NSMutableArray *arr = [self.model.paths mutableCopy];
    if (self.model.video != nil && ![self.model.video isKindOfClass:[NSNull class]] && self.model.video.length > 0) {
        
        [arr addObject:self.model.video];
    }
    
    [_btnImgs enumerateObjectsUsingBlock:^(UIImageView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if ((UIImageView *)sender.view == obj) {
            
            
            self.ClickImgSaveAction ? self.ClickImgSaveAction(arr[idx], (UIImageView *)sender.view) : nil;
            *stop = YES;
        }
    }];
    }
}


#pragma mark - 修改作业
- (IBAction)Modifying:(id)sender {
    if (_callBack) {
        _callBack(self.model.ID);
    }
}


- (UITapGestureRecognizer *)tap {
    
        
        return [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ClickAction:)];

}

- (UILongPressGestureRecognizer *)longPress {
    
    return [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(ClickSaveAction:)];

}
@end
